package com.example.job;

import com.dangdang.ddframe.job.api.ShardingContext;
import com.dangdang.ddframe.job.api.simple.SimpleJob;
import com.example.util.JobParamUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @author YJC
 * @version V1.0
 * @description: TODO
 * @date 2020-11-10 14:34
 */
@Slf4j
@Component("elasticJobDemo")
public class ElasticJobDemo implements SimpleJob {

    @Override
    public void execute(ShardingContext shardingContext) {
        //解析自定义参数
        Map<String, Object> parameter = JobParamUtils.parseJobParameter(shardingContext.getJobParameter());
        log.info("自定义参数解析结果:" + parameter);

        //获取分片参数
        int item = shardingContext.getShardingItem();
        String shardingParameter = shardingContext.getShardingParameter();

        //根据分片选择执行逻辑
        switch (item) {
            case 0:
                log.info(String.format("-----ThreadId:%s,当前分片项：%s-%s",Thread.currentThread().getId(),item, shardingParameter));
                // do something by sharding item 0
                break;
            case 1:
                log.info(String.format("-----ThreadId:%s,当前分片项：%s-%s",Thread.currentThread().getId(),item, shardingParameter));
                // do something by sharding item 1
                break;
            case 2:
                log.info(String.format("-----ThreadId:%s,当前分片项：%s-%s",Thread.currentThread().getId(),item, shardingParameter));
                // do something by sharding item 2
                break;
        }
    }
}

